#ifndef CONFIG_MANAGER_H
#define CONFIG_MANAGER_H

#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>

// 配置参数类型枚举
typedef enum {
    CONFIG_TYPE_INT,
    CONFIG_TYPE_UINT,
    CONFIG_TYPE_BOOL,
    CONFIG_TYPE_STRING,
    CONFIG_TYPE_DOUBLE,
    CONFIG_TYPE_SIZE,     // 内存大小类型 (支持K, M, G后缀)
    CONFIG_TYPE_TIME      // 时间类型 (支持ms, s, m, h后缀)
} config_type_t;

// 配置参数值联合体
typedef union {
    int64_t int_val;
    uint64_t uint_val;
    bool bool_val;
    char *string_val;
    double double_val;
    uint64_t size_val;    // 字节数
    uint64_t time_val;    // 毫秒数
} config_value_t;

// 配置参数结构
typedef struct config_param {
    char name[64];                    // 参数名称
    char description[256];            // 参数描述
    config_type_t type;              // 参数类型
    config_value_t value;            // 当前值
    config_value_t default_value;    // 默认值
    config_value_t min_value;        // 最小值 (数值类型)
    config_value_t max_value;        // 最大值 (数值类型)
    bool readonly;                   // 是否只读
    bool requires_restart;           // 是否需要重启生效
    bool is_set;                     // 是否已设置
    uint64_t last_modified;          // 最后修改时间
    struct config_param *next;       // 链表指针
} config_param_t;

// 配置分组结构
typedef struct config_group {
    char name[64];                   // 分组名称
    char description[256];           // 分组描述
    config_param_t *params;          // 参数链表
    uint32_t param_count;            // 参数数量
    struct config_group *next;       // 链表指针
} config_group_t;

// 配置管理器结构
typedef struct config_manager {
    config_group_t *groups;          // 配置分组链表
    uint32_t group_count;            // 分组数量
    uint32_t total_params;           // 总参数数量
    
    char config_file[256];           // 配置文件路径
    char default_config_file[256];   // 默认配置文件路径
    
    pthread_rwlock_t config_lock;    // 配置读写锁
    bool auto_save;                  // 是否自动保存
    uint64_t last_saved;             // 最后保存时间
    
    // 配置变更回调
    void (*on_config_changed)(const char *param_name, const config_value_t *old_value, 
                              const config_value_t *new_value, void *user_data);
    void *callback_user_data;
} config_manager_t;

// 预定义的配置分组和参数
#define CONFIG_GROUP_GENERAL "general"
#define CONFIG_GROUP_MEMORY "memory"
#define CONFIG_GROUP_STORAGE "storage"
#define CONFIG_GROUP_NETWORK "network"
#define CONFIG_GROUP_LOGGING "logging"
#define CONFIG_GROUP_QUERY "query"
#define CONFIG_GROUP_TRANSACTION "transaction"
#define CONFIG_GROUP_STATISTICS "statistics"

// 配置管理器接口
config_manager_t* config_manager_create(const char *config_file);
void config_manager_destroy(config_manager_t *manager);

// 配置分组操作
int config_add_group(config_manager_t *manager, const char *group_name, 
                     const char *description);
config_group_t* config_get_group(config_manager_t *manager, const char *group_name);
int config_remove_group(config_manager_t *manager, const char *group_name);

// 配置参数定义
int config_define_int(config_manager_t *manager, const char *group_name, 
                     const char *param_name, const char *description,
                     int64_t default_value, int64_t min_value, int64_t max_value,
                     bool readonly, bool requires_restart);

int config_define_uint(config_manager_t *manager, const char *group_name,
                      const char *param_name, const char *description,
                      uint64_t default_value, uint64_t min_value, uint64_t max_value,
                      bool readonly, bool requires_restart);

int config_define_bool(config_manager_t *manager, const char *group_name,
                      const char *param_name, const char *description,
                      bool default_value, bool readonly, bool requires_restart);

int config_define_string(config_manager_t *manager, const char *group_name,
                        const char *param_name, const char *description,
                        const char *default_value, bool readonly, bool requires_restart);

int config_define_double(config_manager_t *manager, const char *group_name,
                        const char *param_name, const char *description,
                        double default_value, double min_value, double max_value,
                        bool readonly, bool requires_restart);

int config_define_size(config_manager_t *manager, const char *group_name,
                      const char *param_name, const char *description,
                      uint64_t default_value, uint64_t min_value, uint64_t max_value,
                      bool readonly, bool requires_restart);

int config_define_time(config_manager_t *manager, const char *group_name,
                      const char *param_name, const char *description,
                      uint64_t default_value, uint64_t min_value, uint64_t max_value,
                      bool readonly, bool requires_restart);

// 配置参数访问
int config_set_int(config_manager_t *manager, const char *param_name, int64_t value);
int config_set_uint(config_manager_t *manager, const char *param_name, uint64_t value);
int config_set_bool(config_manager_t *manager, const char *param_name, bool value);
int config_set_string(config_manager_t *manager, const char *param_name, const char *value);
int config_set_double(config_manager_t *manager, const char *param_name, double value);
int config_set_size(config_manager_t *manager, const char *param_name, uint64_t value);
int config_set_time(config_manager_t *manager, const char *param_name, uint64_t value);

int64_t config_get_int(config_manager_t *manager, const char *param_name, int64_t default_val);
uint64_t config_get_uint(config_manager_t *manager, const char *param_name, uint64_t default_val);
bool config_get_bool(config_manager_t *manager, const char *param_name, bool default_val);
const char* config_get_string(config_manager_t *manager, const char *param_name, const char *default_val);
double config_get_double(config_manager_t *manager, const char *param_name, double default_val);
uint64_t config_get_size(config_manager_t *manager, const char *param_name, uint64_t default_val);
uint64_t config_get_time(config_manager_t *manager, const char *param_name, uint64_t default_val);

// 配置参数查询
config_param_t* config_find_param(config_manager_t *manager, const char *param_name);
bool config_param_exists(config_manager_t *manager, const char *param_name);
bool config_is_readonly(config_manager_t *manager, const char *param_name);
bool config_requires_restart(config_manager_t *manager, const char *param_name);

// 配置文件操作
int config_load_from_file(config_manager_t *manager, const char *filename);
int config_save_to_file(config_manager_t *manager, const char *filename);
int config_reload(config_manager_t *manager);
int config_reset_to_defaults(config_manager_t *manager);

// 配置验证
int config_validate_all(config_manager_t *manager);
int config_validate_param(config_manager_t *manager, const char *param_name);

// 配置监控和回调
int config_set_change_callback(config_manager_t *manager,
                              void (*callback)(const char *param_name, 
                                             const config_value_t *old_value,
                                             const config_value_t *new_value,
                                             void *user_data),
                              void *user_data);

// 配置查询和报告
void config_print_all(config_manager_t *manager);
void config_print_group(config_manager_t *manager, const char *group_name);
void config_print_param(config_manager_t *manager, const char *param_name);
int config_export_to_json(config_manager_t *manager, const char *filename);

// 运行时配置修改
int config_set_from_string(config_manager_t *manager, const char *param_name, 
                          const char *value_str);
char* config_get_as_string(config_manager_t *manager, const char *param_name);

// 配置模板和预设
int config_load_defaults(config_manager_t *manager);
int config_apply_preset(config_manager_t *manager, const char *preset_name);

// 辅助函数
uint64_t config_parse_size(const char *size_str);  // 解析大小字符串 (1K, 2M, 3G)
uint64_t config_parse_time(const char *time_str);  // 解析时间字符串 (100ms, 1s, 2m, 3h)
char* config_format_size(uint64_t size);           // 格式化大小为字符串
char* config_format_time(uint64_t time_ms);        // 格式化时间为字符串
bool config_validate_range(const config_param_t *param, const config_value_t *value);

// 预定义的标准配置参数
void config_init_default_params(config_manager_t *manager);

#endif // CONFIG_MANAGER_H